﻿using C_Sharp_API_Example.src.Advanced.CustomRender;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using df = System.Windows.Forms;
using dw = System.Drawing;

namespace C_Sharp_API_Example
{
    public class VideoControl : df.UserControl
    {
        public df.FlowLayoutPanel PnlContianer;
        public VideoControl VCenter;
        public bool IsOpenVideo = false;
        public bool IsOpenAudio = false;
        public bool IsJoined = false;
        public uint Uid { get; set; }
        public string Token { get; set; }
        public string ChannelId { get; set; }

        public string UserName { get; set; }

        private BaseRender render_;

        public BaseRender Render { get => render_; private set { } }

        public Action<VideoControl> ChangeCenterEvent;

        public VideoControl(df.FlowLayoutPanel p,VideoControl center,string token,string channelId,uint uid)
        {
            PnlContianer = p;
            this.BackColor = dw.Color.FromArgb(0, 0, 0);
            VCenter = center;
            this.Uid = uid;
            this.Token = token;
            ChannelId = channelId;
            rec = new dw.Rectangle(10, this.Height - 20, 50, 50);
        }

        public void Add()
        {
            if (PnlContianer.InvokeRequired)
            {
                PnlContianer.Invoke(new Action(() =>
                {
                    PnlContianer.Controls.Add(this);
                }));
            }
            else
            {
                PnlContianer.Controls.Add(this);
            }
        }
        private object obj_ = new object();

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);
            if(VCenter!=null&&VCenter.Uid!=this.Uid)
            {
                VCenter.Uid=this.Uid;
                VCenter.IsOpenAudio = this.IsOpenAudio;
                VCenter.IsOpenVideo = this.IsOpenVideo;
                if(this.IsOpenVideo==false)
                {
                    VCenter.ClearBackground();
                }
                VCenter?. ChangeCenterEvent?.Invoke(this);
            }
        }

        private void ChangeCenter()
        {

        }

        public void LiveChannel()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => {
                    PnlContianer.Controls.Remove(this);
                }));
            }
            else
            {
                PnlContianer.Controls.Remove(this);
            }
        }

        public void DisableVideo()
        {
            this.ClearBackground();
        }
        bool isClear=false;
        public void ClearBackground()
        {
            
            var g=this.CreateGraphics();

            g.Clear(dw.Color.FromArgb(0, 0, 0));
            g.Dispose();

        }

        public void DestroyRender()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => {
                    if (render_ != null)
                        render_.Dispose();
                    render_ = null;
                }));
            }
            else
            {
                if (render_ != null)
                    render_.Dispose();
                render_ = null;
            }
        }
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            lock (obj_)
            {
                if (render_ != null)
                {
                    if (render_.NeedInvoke())
                    {
                        this.Invoke(new Action(() =>
                        {
                            render_.UpdateSize(this.Size);
                        }));
                    }
                    else
                    {
                        render_.UpdateSize(this.Size);
                    }
                }
            }
        }
        private dw.Rectangle rec;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
        }

        private void ReInitRender()
        {

            if (render_ != null)
            {
                return;
            }

            if (render_ != null)
            {
                render_.Dispose();
                render_ = null;
            }
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.UserPaint, false);
            render_ = new SharpDXRender();
         
            BaseRender.DataType data_type = BaseRender.DataType.kBGRA;


            if (render_ != null && render_.Initialize(this.Handle, this.Size, data_type) != true)
            {
                render_.Dispose();
                render_ = null;
            }
        }

        public void DeliverFrame(Agora.Rtc.VideoFrame videoFrame)
        {
            lock (obj_)
            {
                if (render_ == null)
                {
                    this.Invoke(new Action(() =>
                    {
                        ReInitRender();
                    }));
                }

                if (render_.NeedInvoke())
                {
                    this.Invoke(new Action(() =>
                    {
                        render_.DeliverFrame(videoFrame);
                    }));
                }
                else
                {
                   
                    render_.DeliverFrame(videoFrame);
                }
            }
        }
    }
}
